﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ColorfulStringBuilder
{
    internal class ColorfulToken
    {
        public readonly string Content;
        public readonly ConsoleColor? BackgroundColor;
        public readonly ConsoleColor? ForegroundColor;

        public ColorfulToken(string content, ConsoleColor? backgroundColor, ConsoleColor? foregroundColor)
        {
            Content = content;
            BackgroundColor = backgroundColor;
            ForegroundColor = foregroundColor;
        }
    }

    internal class ColorfulTokenLine : List<ColorfulToken> { }

    internal class ColorfulTokenLines : List<ColorfulTokenLine> { }

    public class ColorfulStringBuilder
    {
        private ColorfulTokenLines _cache = new ColorfulTokenLines();

        #region methods

        private void PrepareAppend()
        {
            if (_cache.Count == 0) _cache.Add(new ColorfulTokenLine());
        }

        #endregion

        #region append

        public ColorfulStringBuilder Append(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null)
        {
            if (string.IsNullOrEmpty(value)) return this;
            PrepareAppend();
            _cache.Last().Add(new ColorfulToken(value, backgroundColor, foregroundColor));
            return this;
        }

        #endregion

        #region append line

        public ColorfulStringBuilder AppendLine(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null)
        {
            if (string.IsNullOrEmpty(value)) return this;
            PrepareAppend();
            _cache.Last().Add(new ColorfulToken(value, backgroundColor, foregroundColor));

            _cache.Add(new ColorfulTokenLine());

            return this;
        }

        #endregion

        #region methods

        public void OutputToConsole()
        {
            var originBackgroundColor = Console.BackgroundColor;
            var originForegroundColor = Console.ForegroundColor;


            foreach (var line in _cache)
            {
                foreach (var token in line)
                {
                    if (string.IsNullOrEmpty(token.Content)) continue;

                    var backgroundColor = token.BackgroundColor ?? originBackgroundColor;
                    var foregroundColor = token.ForegroundColor ?? originForegroundColor;
                    Console.BackgroundColor = backgroundColor;
                    Console.ForegroundColor = foregroundColor;
                    Console.Write(token.Content);
                }

                Console.BackgroundColor = originBackgroundColor;
                Console.ForegroundColor = originForegroundColor;
                Console.WriteLine();
            }

            Console.BackgroundColor = originBackgroundColor;
            Console.ForegroundColor = originForegroundColor;
        }

        #endregion

        #region overrides
        public override string ToString()
        {
            var sb = new StringBuilder();
            foreach (var line in _cache)
            {
                foreach (var token in line)
                {
                    sb.Append(token.Content);
                }
                sb.AppendLine();
            }
            return sb.ToString();
        }
        #endregion


    }
}
